//
//  APTagsView.swift
//  APSwift
//
//  Created by jianbo.pei on 2022/9/13.
//

import UIKit

protocol APTagsModelProtocol {
    /// 缓存tagName的宽
    var tagNameWidth: CGFloat? { get }
    /// 缓存tagItem的宽
    var tagItemWidth: CGFloat? { get }
}

protocol APTagsViewDataSource: AnyObject {
    func tagsView(_ tagsView: APTagsView, cellFotItemAt index: Int) -> APTagsViewCell
}

protocol APTagsViewDelegate: AnyObject {
    func tagsView(_ tagsView: APTagsView, didTapped tagModel: APTagsModelProtocol, at index: Int)
}

extension APTagsModelProtocol {
    func tagsView(_ tagsView: APTagsView, didTapped tagModel: APTagsModelProtocol, at index: Int) {}
}

class APTagsView: UIView {
    
    public struct APTagsViewKey {
        static var cellIdentifier = "k_APTagsView_identifier"
    }
    
    public var tagModels: [APTagsModelProtocol]? {
        didSet {
            collectionView.reloadData()
        }
    }

    private var collectionView: UICollectionView!
    public weak var dataSource: APTagsViewDataSource?
    public weak var delegate: APTagsViewDelegate?
    
    /// tag的高度，默认30
    public var tagHeight: CGFloat = 30
    public var minimumLineSpacing: CGFloat = 10
    public var minimumInteritemSpacing: CGFloat = 10
    public var contentInsets: UIEdgeInsets = UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5)
    public var minTagWidth: CGFloat = 12
    public var maxTagWidth: CGFloat = 200

    override init(frame: CGRect) {
        super.init(frame: frame)
        initUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func initUI() {
        let flowLayout = APTagsFlowLayout()
        flowLayout.delegate = self
        
        collectionView = UICollectionView(frame: bounds, collectionViewLayout: flowLayout)
        collectionView.dataSource = self
        collectionView.delegate = self
        addSubview(collectionView)
    }
}

extension APTagsView: UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    
    //MARK: UICollectionViewDataSource
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return tagModels?.count ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        if let cell = dataSource?.tagsView(self, cellFotItemAt: indexPath.item) {
            return cell
        } else {
            return UICollectionViewCell(frame: .zero)
        }
    }
    
    
    //MARK: UICollectionViewDelegateFlowLayout
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        if let tagModels = tagModels, tagModels.count > indexPath.item {
            let tagModel = tagModels[indexPath.item]
            if var tagW = tagModel.tagItemWidth, tagW > 0 {
                if tagW < minTagWidth {
                    tagW = minTagWidth
                }
                if tagW > maxTagWidth {
                    tagW = maxTagWidth
                }
                return CGSize(width: tagW, height: tagHeight)
            }
        }
        return .zero
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return minimumLineSpacing
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return minimumInteritemSpacing
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return contentInsets
    }
}
